<template>
	<view class="page-body">
		<u-navbar :is-back="true" title="充值保证金"></u-navbar>
		<view class="money-box text-center">
			￥{{model.money}}
		</view>
		<view class="pay-box bg-white">
			<view class="pay-way">
				<view class="fs32">支付方式</view>
				<u-grid :col="3" :border="false" class="u-p-t-30" hover-class="none">
					<u-grid-item v-for="(item,index) in paySetting.payList" :key="index" @click="pay_code=item.pay_code" :custom-style="{padding:'0 10rpx'}">
						<view class="item" :class="pay_code == item.pay_code ? 'checked': ''">
							<u-icon :name="payImgList[item.pay_code]" :size="48"></u-icon>
							<view class="grid-text u-p-t-20 u-font-24">{{item.pay_name}}</view>
						</view>
					</u-grid-item>
				</u-grid>
			</view>
			<view v-if="pay_code=='offline'" class="account-box u-m-t-40">
				<view class="fs32">上级收款账户</view>
				<view class="section-cell u-m-t-30">
					<u-section :title="sectionTitle" :sub-title="sectionSubTitle" :subColor="sectionSubColor" @click="showAccount" :showLine="false"></u-section>
				</view>
				<view>
					<u-select v-model="showAccountSelect" :list="offlinePayList" @confirm="selectOffline" confirm-color="#C39F4F"></u-select>
				</view>
				<view>
					<u-cell-group>
						<block v-if="paySetting.offline.weixin_usd == 1 && selectOfflinePay == 1">
							<u-cell-item title="帐户姓名" :value="paySetting.offline.weixin_name" :arrow="false" hover-class="none"></u-cell-item>
							<u-cell-item title="微信号" :value="paySetting.offline.weixin_account" :arrow="false" hover-class="none"></u-cell-item>
							<u-cell-item title="收款二维码" :use-label-slot="true" :border-bottom="false" :arrow="false" hover-class="none">
								<view slot="label">
									<u-image width="600" mode="widthFix" :src="baseUrl+paySetting.offline.weixin_qrcode"></u-image>
								</view>
							</u-cell-item>
						</block>
						<block v-if="paySetting.offline.alipay_usd == 1 && selectOfflinePay == 2">
							<u-cell-item title="帐户姓名" :value="paySetting.offline.alipay_name" :arrow="false" hover-class="none"></u-cell-item>
							<u-cell-item title="支付宝帐户" :value="paySetting.offline.alipay_account" :arrow="false" hover-class="none"></u-cell-item>
							<u-cell-item title="收款二维码" :use-label-slot="true" :border-bottom="false" :arrow="false" hover-class="none">
								<view slot="label">
									<u-image width="600" mode="widthFix" :src="baseUrl+paySetting.offline.alipay_qrcode"></u-image>
								</view>
							</u-cell-item>
						</block>
						<block v-if="paySetting.offline.bank_usd == 1 && selectOfflinePay == 3">
							<u-cell-item title="开户银行" :value="paySetting.offline.bank_name" :arrow="false" hover-class="none"></u-cell-item>
							<u-cell-item title="开户支行" :value="paySetting.offline.bank_subbranch" :arrow="false" hover-class="none"></u-cell-item>
							<u-cell-item title="持卡人姓名" :value="paySetting.offline.bank_user_name" :arrow="false" hover-class="none"></u-cell-item>
							<u-cell-item title="银行卡号" :value="paySetting.offline.bank_card_number" :arrow="false" hover-class="none"></u-cell-item>
						</block>
						
					</u-cell-group>
				</view>
			</view>
			<view>
				<block v-if="pay_code=='offline'" >
					<u-cell-item title="上传付款凭证（最多三张）" 
						class="u-p-l-0 u-p-r-0" 
						:use-label-slot="true" 
						:title-style="{fontSize:'32rpx'}"
						:border-bottom="false" 
						:arrow="false" 
						hover-class="none">
						<view slot="label">
							<u-upload ref="upload" @on-change="uploaded" @on-remove="removeImg" :action="upAction" max-count="3"></u-upload>
						</view>
					</u-cell-item>
				</block>
			</view>
			<view class="section-cell u-m-t-30" @click="app.goPage('/pagesB/channel/contract/index?proxy_id='+proxy_id)">
				<u-section :title="isSignContract ? '电子合同' : '前往签约电子合同'" :sub-title="isSignContract ? '已签约' : '未签约'" :showLine="false"></u-section>
			</view>
			
			<view>
				<u-button v-if="pay_code=='offline'" class="defaultbtn mt20" shape="circle" :loading="isLoading" @click="postOffLine">立即提交</u-button>
				
				<u-button v-if="pay_code!='' && pay_code!='offline'" class="mt20 defaultbtn" shape="circle"  :loading="isLoading"
				 @click="pay">立即支付</u-button>
			</view>
		</view>
		
		
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: this.config.baseUrl,
				upAction: this.config.upImageUrl,
				from: 'channel',
				payType:'recharge',
				order_id:0,
				paySetting: {
					offline: {
						weixin_usd: 0,
						alipay_usd: 0,
						bank_usd: 0
					}
				},
				isLoading:false,
				title:'充值',
				showRemark:false,
				isDisabledMoney:false,
				model: {
					money: '',
					remark:'',
					type:''
				},
				errorType: ['toast'],
				rules:{
					money: [{
							required: true,
							message: '请输入需要充值的金额.',
							trigger: ['change', 'blur'],
						},
						{
							type: 'number',
							message: '只能输入整数.',
							trigger: ['change', 'blur'],
						}
					]
				},
				proxy_id:0,
				supply_headimgurl: this.config.baseUrl + '/static/public/images/headimgurl.jpg',
				proxyInfo:{
					byUser:{},
					proxyLevel:{}
				},
				payImgList: {
					balance: '/static/public/images/pay/balance.png',
					weixin: '/static/public/images/pay/weixin.png',
					appWeixinPay: '/static/public/images/pay/weixin.png',
					miniAppPay: '/static/public/images/pay/weixin.png',
					alipayMobile: '/static/public/images/pay/alipay.png',
					alipayApp: '/static/public/images/pay/alipay.png',
					offline: '/static/public/images/pay/bank.png',
					goodsMoney: '/static/public/images/pay/goodsmoney.png',
					uplevelGoodsMoney: '/static/public/images/pay/goodsmoney.png',
				},
				pay_code: 'offline',
				showAccountSelect: false,
				offlinePayList:[],
				sectionTitle: '上级未设置收款账户',
				sectionSubTitle: '电话联系上级',
				sectionSubColor: '#C39F4F',
				selectOfflinePay: 0,
				proxyLevelInfo: {},
				isSignContract: false,
				fileList:[]
			}
		},
		onLoad(options) {
			this.proxy_id = parseInt(options.proxy_id);
			this.model.type = options.type;
			if (this.model.type == 'earnestMoney'){
				if (options.money > 0){
					this.model.money = options.money;
					this.isDisabledMoney = true;
				}
				this.title = '需缴纳保证金';
			}else if (this.model.type == 'goodsMoney'){
				this.title = '充值货款';
			}else if (this.model.type == 'uplevelGoodsMoney'){
				this.title = '充值升级货款';
				if (options.money > 0){
					this.model.money = options.money;
					this.isDisabledMoney = true;
				}
			}
			uni.setNavigationBarTitle({
			　　title:this.title
			})
			this.getRechargePayInfo();
			this.getUpgradeLevel();
			this.getContract();
		},
		onShow() {
			//获取登录会员信息
			this.$u.api.getProxyInfo().then(res => {
				this.proxyInfo = res.data.proxyInfo;
				if(this.proxyInfo.supply_uid < 1){
					this.supply_headimgurl = this.config.baseUrl + '/static/public/images/headimgurl.jpg';
				} else {
					this.supply_headimgurl = this.proxyInfo.supply_headimgurl == '' ?
							this.config.baseUrl + 'static/public/images/headimgurl.jpg' :
							this.config.baseUrl+this.proxyInfo.supply_headimgurl;
				}
			});
			this.getContract();
		},
		onReady() {
			//this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			showAccount(){
				if(this.paySetting.offline.is_usd == 1){
					this.showAccountSelect = true;
				}else{
					let that = this;
					uni.makePhoneCall({
					    phoneNumber: that.proxyInfo.supply_mobile
					});
				}
			},
			getRechargePayInfo() { //获取充值支付选项信息
				let platform = this.app.getPlatform();
				let proxy_id = this.proxy_id;
				this.$u.post('channel/api.wallet/getRechargePayInfo',{platform: platform,proxy_id:proxy_id}).then(res => {
					this.paySetting = res.data;
					this.setOfflinePay();
				}).catch(res => {
					this.app.showModal('请求错误', '/pagesB/channel/center/index', true);
				})
			},
			getUpgradeLevel(){//获取可升级的层级
				let proxy_id = this.proxy_id;
				let proxy_info = {};
				this.$u.post('channel/api.proxy_users/getUpgradeLevel').then(res => {
					if (res.data.upList.length > 0){
						res.data.upList.forEach(function(item){
							if(item.proxy_id == proxy_id){
								proxy_info = item;
								return true;
							}
						});
						if(proxy_info.diff_earnest_money > 0){
							this.model.money = proxy_info.diff_earnest_money;
						}
						this.proxyLevelInfo = proxy_info;
					}
				});
			},
			selectOffline(val){
				this.selectOfflinePay = val[0].value != null ? val[0].value : this.selectOfflinePay;
				this.sectionTitle = val[0].label != null ? val[0].label : this.sectionTitle;
			},
			setOfflinePay(){
				if(this.paySetting.offline.weixin_usd == 1){
					this.offlinePayList.push({
						value: '1',
						label: '微信转帐'
					});
				}
				if(this.paySetting.offline.alipay_usd == 1){
					this.offlinePayList.push({
						value: '2',
						label: '支付宝转帐'
					});
				}
				if(this.paySetting.offline.bank_usd == 1){
					this.offlinePayList.push({
						value: '3',
						label: '银行转帐'
					});
				}
				if(this.paySetting.offline.is_usd == 1){
					this.sectionTitle = '请选择上级代理的收款账户';
					this.sectionSubTitle = '查看全部账户';
					this.sectionSubColor = '#999999';
				} 
			},
			postOffLine() {
				if (this.isLoading == true) {
					return false;
				}
				if (this.fileList.length < 1) {
					this.$u.toast('请上传付款凭证.');
					return false;
				}
				this.isLoading = true;
				let order_id = this.order_id;
				let payType = this.payType;
				let from = this.from;
				let url = '';
				let data = {};
				
				url = 'channel/api.wallet/postRecharge';
				data = this.model;
				if (data.money == '' || data.money <= 0) {
					this.isLoading = false;
					return this.$u.toast('请输入充值金额.');
				}
				data.fileList = this.fileList.join(',');
				data.payCode = 'offline';
				data.proxy_id = this.proxy_id;
				data.upgrade_type = 2;
				data.order_type = 1;
				this.$u.post(url, data).then(res => {
					this.isLoading = false;
					uni.redirectTo({
						url: `/pages/public/pay/paySuccess?from=${from}&payType=${payType}&order_id=${order_id}&pay_code=offline`
					});
				}).catch(res => {
					this.isLoading = false;
				})
			
			},
			async pay() {
				if (this.isLoading == true) {
					return false;
				}
				this.isLoading = true;
				let route = '/pages/public/pay/paySuccess';
				let order_id = this.order_id;
				let from = this.from;
				let url = '';
				//充值处理，先创建充值订单，返回订单ID
				if (this.payType == 'recharge') {
					let data = this.model;
					if (data.money == '' || data.money <= 0) {
						this.isLoading = false;
						return this.$u.toast('请输入充值金额.');
					}
					data.payCode = this.pay_code;
					if (from == 'member') {
						url = 'member/api.wallet/postRecharge';
					} else if (from == 'channel') {
						data.proxy_id = this.proxy_id;
						url = 'channel/api.wallet/postRecharge';
					} else {
						return this.$u.toast('请求错误.');
					}
					let res = await this.$u.post(url, data).catch(res => {})
					this.isLoading = false;
					order_id = res.data.order_id;
					if (order_id < 1) return false;
				}
				let source = this.app.getPlatform();
				if (source == 'H5-WX'){
					window.location.href = this.baseUrl+'pages/public/pay/payIng?from=' + this.from + '&pay_code=' + this.pay_code + '&payType=' + this.payType +
						'&order_id=' + order_id + '&route=' + route;
				}else{
					this.app.goPage('/pages/public/pay/payIng?from=' + this.from + '&pay_code=' + this.pay_code + '&payType=' + this.payType +
						'&order_id=' + order_id + '&route=' + route,-1)
				}
					
				this.isLoading = false;
			},
			uploaded(res, index, lists) {
				var res = JSON.parse(res.data);
				this.fileList.push(res.file);
			},
			removeImg(index, lists) {
				let file = this.fileList[index];
				this.fileList.splice(index, 1);
				this.$u.post(this.config.removeImageUrl, {
					file: file
				}).then(res => {})
			},
			getContract() { //获取合同信息
				let proxy_id = this.proxy_id;
				this.$u.post('channel/api.proxy_users/getContract',{proxy_id:proxy_id}).then(res => {
					let contract = res.data.contract;
					if(contract.is_signature == 1){
						this.isSignContract = true;
					}
				}).catch(res => {
					//this.app.showModal('请求错误', '/pagesB/channel/center/index', true);
				})
			},
		}
	}
</script>

<style lang="scss">
	.money-box{
		height: 200rpx;
		line-height: 200rpx;
		font-size: 66rpx;
	}
	.pay-box{
		min-height: calc(100vh - 288rpx);
		padding: 40rpx 20rpx;
		border-radius: 40rpx 40rpx 0px 0px;
		
		.item {
			padding: 30rpx 10rpx;
			width: 100%;
			height: 100%;
			text-align: center;
			border: 2rpx solid #ccc;
			border-radius: 8rpx;
			&.checked{
				background: #F9F5ED;
				border: 2rpx solid #744800;
			}
		}
		
	}
	
	.section-cell{
		padding: 20rpx;
		border-radius: 8rpx;
		background: #ececec;
	}
	.form_box {
		padding: 80rpx 40rpx;
		.title {
			font-size: 35rpx;
		}
		._input {
			border-radius: 10rpx;
			display: flex;
			background-color: $border-color-light;
			margin-top: 40rpx;
			padding: 20rpx;
			font-size: 38rpx;
		}
		.remark_input{
			margin-top: 20rpx;
		}
		.addRemark{
			margin-top: 30rpx;
			color: $font-color-base;
			.u-icon{
				border: 1rpx solid $font-color-base;
				border-radius: 50%;
				font-size: 20rpx;
				padding: 2rpx;
				margin-right: 5rpx;
				line-height: 50rpx;
			}
		}
	}
	.superior_box{
		position: relative;
		display: block;
		overflow: hidden;
		.headimgurl {
			width: 100rpx !important;
			height: 100rpx !important;
			border: 5rpx solid #FFFFFF;
		}
		.level{
			margin-left: 10rpx;
			font-size: 24rpx;
			color: $base-color;
			border-radius: 10rpx;
			padding: 4rpx 10rpx;
			border: 1rpx solid $base-color;
		}
		.persql{
			font-size: 23rpx;
			background: #5392f3;
			color: #FFFFFF;
		    position: absolute;
		    z-index: 9;
			padding: 0rpx 50rpx;
		    top: 28rpx;
		    right: -63rpx;
		    -webkit-transform: rotate(50deg);
		    transform: rotate(50deg);
		}
	}
	
	.defaultbtn[disabled="true"]{
		background-color: #c5c5c5 !important;
	}
</style>
